package com.medical.admin.sync;

import com.alibaba.fastjson.JSONObject;
import com.medical.admin.constant.Constant;
import com.medical.admin.domain.Consult;
import com.medical.admin.es.ConsultEs;
import com.medical.admin.esMapper.ConsultEsMapper;
import com.medical.admin.service.ConsultService;
import com.medical.common.core.utils.bean.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @BelongsProject: Medical
 * @BelongsPackage: com.medical.consult.sync
 * @Author: LX
 * @CreateTime: 2025-02-26  22:10
 * @Description: TODO
 * @Version: 1.0
 */

@Component
@Slf4j
public class ConsultSyncService implements ApplicationRunner {

    @Autowired
    private ConsultService consultService;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private ConsultEsMapper consultEsMapper;
    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.info("咨询:咨询输出初始化到内存中,开始!");
        List<Consult> list = consultService.list();
        redisTemplate.opsForValue().set(Constant.SYNC_CONSULT_LIST, JSONObject.toJSONString(list),20, TimeUnit.MINUTES);
        log.info("咨询:初始化结束{}",list);

        log.info("咨询:咨询输出初始化到es中,开始!");
        List<ConsultEs> consultEsList = new ArrayList<>();
        for (Consult consult : list) {
            ConsultEs consultEs = new ConsultEs();
            BeanUtils.copyBeanProp(consultEs,consult);
            consultEsList.add(consultEs);
        }
        log.info("开始将数据添加到es中");
        consultEsMapper.insertBatch(consultEsList);
        log.info("咨询:项目初始化到es结束!");
    }

}
